#include "calServer.hpp"
#include <memory>

void Usage(const char *proc)
{
    std::cout << "Usage:\n\t" << proc << " local_port" << std::endl;
}

void Cal(const Request &request, Response *response)
{

    switch (request.op)
    {
    case '+':
        response->result = request.x + request.y;
        break;
    case '-':
        response->result = request.x + request.y;
        break;
    case '*':
        response->result = request.x * request.y;
        break;
    case '/':
        if(request.y == 0)
            response->exitcode =  DIV_ZERO;
        else
            response->result = request.x / request.y;
        break;
    case '%':
        if(request.y == 0)
            response->exitcode = MOD_ZERO;
        else
            response->result = request.x % request.y;
        break;
    dfeault:
        response->exitcode = OP_ERROR;
        break;
    }
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }

    uint16_t port = atoi(argv[1]);
    std::unique_ptr<calServer> tsevr(new calServer(port));
    tsevr->initServer();

    tsevr->start(Cal);

    return 0;
}